package com.wrt.wm.util;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j2;

@Component
@Log4j2
public class CleanProjectFileUtil {

	@Value("${temppath.temp:temp}")
	private String temp;

	@Value("${temppath.wm:temp_wm}")
	private String wm;

	@Value("${temppath.source:temp_source}")
	private String sourcepath;

	@Autowired
	private DeleteFileUtil deleteFile;

	/**
	 * 删除项目中的所有文件夹路径下的文件
	 */
	public void cleanAll() {
		File tempFile = new File(temp);
		cleanDirectory(tempFile);
		File wmFile = new File(wm);
		cleanDirectory(wmFile);
		File sourceFile = new File(sourcepath);
		cleanDirectory(sourceFile);
		log.info("文件夹已清理");
	}

	/**
	 * 清理文件夹
	 * 
	 * @param dir 文件夹对象
	 */
	private void cleanDirectory(File dir) {
		if (dir.exists() && dir.isDirectory()) {
			File[] allFiles = dir.listFiles();
			if (allFiles.length != 0) {
				deleteFile.cleanUp(allFiles);
			}
		}
	}
}
